package org.yys.demo;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.yys.constant.MqConstant;

import java.io.IOException;
import java.util.Date;
import java.util.List;

public class RetryDemo {


    public void retryProducer() throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        // 创建生产者
        DefaultMQProducer producer = new DefaultMQProducer("retry-producer-group");
        producer.setNamesrvAddr(MqConstant.NAMESRV_ADDR);
        // 发送给mq的失败次数上限, 一般不用额外设置
        producer.setRetryTimesWhenSendFailed(2);
        producer.start();

        // 创建消息
        Message message = new Message("retry-topic", "这是一个简单的消息C3".getBytes());

        // 发送消息
        SendResult send = producer.send(message);
        System.out.println("发送消息成功."+new Date());


        producer.shutdown();

    }

    public void retryConsumer() throws MQClientException, InterruptedException, IOException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("retry-consumer-group");
        consumer.setNamesrvAddr(MqConstant.NAMESRV_ADDR);
        consumer.subscribe("retry-topic","*");
        consumer.setMaxReconsumeTimes(1);
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                MessageExt messageExt = list.get(0);
                System.out.println("消费者接收消息..."+new Date());
                System.out.println("重试次数:"+messageExt.getReconsumeTimes());
                System.out.println("message:"+new String(messageExt.getBody()));
//                System.out.println("context:"+consumeConcurrentlyContext);
                return ConsumeConcurrentlyStatus.RECONSUME_LATER;
            }
        });

        consumer.start();


        System.in.read();

    }

    public void dlqConsumer() throws MQClientException, InterruptedException, IOException {

        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("dlq-consumer-group");
        consumer.setNamesrvAddr(MqConstant.NAMESRV_ADDR);
        // 特别注意: 这个topic需要在控制台手动将perm设置为6, 这样才可读!!!
        consumer.subscribe("%DLQ%retry-consumer-group","*");

//        consumer.setMaxReconsumeTimes(2);
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                MessageExt messageExt = list.get(0);
                System.out.println("死信消费者接收消息..."+new Date());
                System.out.println("重试次数:"+messageExt.getReconsumeTimes());
                System.out.println("message:"+new String(messageExt.getBody()));
//                System.out.println("context:"+consumeConcurrentlyContext);
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

        consumer.start();
        System.out.println("死信消费者开启...");


        System.in.read();

    }



}
